package com.jxs.android.weiget;

import java.util.Date;

import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.ges.R;
import com.jxs.android.util.OpTimer;
import com.jxs.android.util.StringUtils;
import com.jxs.android.util.Timer;

public class Bu_Filter_Page_Time_View extends LinearLayout implements OnClickListener {

	private TextView filter_time_this_week, filter_time_this_month, filter_time_last_3_month, filter_time_all;

	private EditText startTimeEt, endTimeEt;

	public Bu_Filter_Page_Time_View(Context context) {
		this(context, null);
	}

	public Bu_Filter_Page_Time_View(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.bu_filter_page_time_view, this);
		initFilterRadioGroup();
	}

	private void initFilterRadioGroup() {
		filter_time_this_week = (TextView) findViewById(R.id.filter_time_this_week);
		filter_time_this_month = (TextView) findViewById(R.id.filter_time_this_month);
		filter_time_last_3_month = (TextView) findViewById(R.id.filter_time_last_3_month);
		filter_time_all = (TextView) findViewById(R.id.filter_time_all);

		filter_time_this_week.setText(Html.fromHtml("<a href='#'>" + filter_time_this_week.getText().toString() + "</a>"));
		filter_time_this_month.setText(Html.fromHtml("<a href='#'>" + filter_time_this_month.getText().toString() + "</a>"));
		filter_time_last_3_month.setText(Html.fromHtml("<a href='#'>" + filter_time_last_3_month.getText().toString() + "</a>"));
		filter_time_all.setText(Html.fromHtml("<a href='#'>" + filter_time_all.getText().toString() + "</a>"));

		startTimeEt = (EditText) findViewById(R.id.start_time);
		endTimeEt = (EditText) findViewById(R.id.end_time);
		startTimeEt.setFocusable(false);
		endTimeEt.setFocusable(false);

		startTimeEt.setOnClickListener(clickListener);
		endTimeEt.setOnClickListener(clickListener);

		filter_time_this_week.setOnClickListener(this);
		filter_time_this_month.setOnClickListener(this);
		filter_time_last_3_month.setOnClickListener(this);
		filter_time_all.setOnClickListener(this);

	}

	private View.OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v instanceof EditText) {
				String defaultTime = Timer.getSDFyyyyMMdd().format(new Date());
				if (!StringUtils.isEmpety(((EditText) v).getText().toString())) {
					defaultTime = ((EditText) v).getText().toString();
				}
				showDateDialog(v.getId(), defaultTime);
			}
		}
	};

	private void showDateDialog(int request, String defaultTime) {
		WheelDateDialog dateDialog = new WheelDateDialog(getContext(), request, defaultTime);
		dateDialog.setDateChangeListener(new WheelDateDialog.DateChangeListener() {

			@Override
			public void onKeySave(int requestCode, String dateTimeValue) {
				if (requestCode == startTimeEt.getId()) {
					startTimeEt.setText(dateTimeValue);
				}
				if (requestCode == endTimeEt.getId()) {
					endTimeEt.setText(dateTimeValue);
				}
			}
		});
		dateDialog.show();
	}

	@Override
	public void onClick(View v) {
		int index = Integer.valueOf(v.getTag().toString());
		OpTimer opTimer = new OpTimer(index);
		setStartTime(opTimer.getStartTime());
		setEndTime(opTimer.getEndTime());
	}

	public String getStartTime() {
		return startTimeEt.getText().toString();
	}

	public String getEndTime() {
		return endTimeEt.getText().toString();
	}

	public void setStartTime(String startTime) {
		this.startTimeEt.setText(startTime);
	}

	public void setEndTime(String endTime) {
		this.endTimeEt.setText(endTime);
	}

}
